package com.wnhz.work.jy.activity;

import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.MyUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class ChangeActivity extends BaseActivity implements View.OnClickListener, TextWatcher {
    private static final String TAG = "ChangeActivity";
    private TextView title;
    private View rl_phone;
    private View passWork;

    private EditText et_phone;
    private EditText et_passWord_one;
    private EditText et_passWord_two;
    private EditText et_passWord_old;
    private EditText verification_code;
    private String passWord_one;
    private String passWord_two;
    private String passWord_old;
    private String phone;
    private String code;

    private TextView get_verification_code_tv;

    private String type;
    private int time = 0;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 999:
                    time--;
                    if (time == 0) {
                        get_verification_code_tv.setText("获取验证码");
                        setba();
                        return;
                    }

                    get_verification_code_tv.setText("重新获取" + time);
                    get_verification_code_tv.setSelected(false);
                    handler.sendEmptyMessageDelayed(999, 1000);

                    break;

                default:
                    break;
            }
        }

    };

    private void setba() {
        get_verification_code_tv.setSelected(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change);
        type = getIntent().getStringExtra("type");
        initView();
        initTirle();
        et_phone.addTextChangedListener(this);
        if ("更换登录密码".equals(type)) {
            passWork.setVisibility(View.VISIBLE);
            rl_phone.setVisibility(View.GONE);
        } else {
            rl_phone.setVisibility(View.VISIBLE);
            passWork.setVisibility(View.GONE);
        }
        get_verification_code_tv.setOnClickListener(this);
    }

    private void initView() {


        rl_phone = findViewById(R.id.rl_phone);
        passWork = findViewById(R.id.passWork);
        get_verification_code_tv = (TextView) findViewById(R.id.get_verification_code);

        et_phone = (EditText) findViewById(R.id.et_phone);
        et_passWord_one = (EditText) findViewById(R.id.et_passWord_one);
        et_passWord_two = (EditText) findViewById(R.id.et_passWord_two);
        et_passWord_old = (EditText) findViewById(R.id.et_passWord_old);
        verification_code = (EditText) findViewById(R.id.verification_code);

        findViewById(R.id.qd).setOnClickListener(this);
    }

    private void initTirle() {
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(type);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qd:
                passWord_one = et_passWord_one.getText().toString();
                passWord_two = et_passWord_two.getText().toString();
                passWord_old = et_passWord_old.getText().toString();
                phone = et_phone.getText().toString();
                code = verification_code.getText().toString();
                change();
                break;
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.get_verification_code:
                phone = et_phone.getText().toString();
                if (MyUtils.isMobileNO(phone)) {
                    getCode(phone);
                } else {
                    MyToast("请输入正确手机号");
                }
                break;
        }
    }

    private void change() {
        Map<String, Object> map = new HashMap<>();
        if ("更换登录密码".equals(type)) {

            /*if (passWord_old == null || "".equals(passWord_old)){

            }else */
            if (passWord_one == null || "".equals(passWord_one)) {

                MyToast("请输入密码");
            } else if (passWord_two == null || "".equals(passWord_two)) {
                MyToast("请再次输入密码");
            } else if (passWord_two.length() < 6) {
                MyToast("密码不能少于6位");
            } else if (passWord_two.equals(passWord_one)) {
                map.put("userId", MyApplication.getInstance().userBean.getUserId());
                map.put("password", passWord_one);
                map.put("rePassword", passWord_two);
                changeBinding(map, ConfigUtil.CHANGEPASSWORD);
            } else {
                MyToast("两次次输入的密码不相同");
            }
        } else {
            if (!MyUtils.isMobileNO(phone)) {
                MyToast("请输入正确手机号");
            } else if (code == null || "".equals(code)) {
                MyToast("请输入验证码");
            } else if (!phone.equals(oldphone)) {
                MyToast("验证码与手机号不匹配");
            } else if (!code.equals(first_code)) {
                MyToast("验证码错误");
            } else {
                map.put("bindTel", phone);
                map.put("userId", MyApplication.getInstance().userBean.getUserId());
                changeBinding(map, ConfigUtil.CHANGEBINDING);
            }
        }
    }

    private void changeBinding(Map<String, Object> map, String url) {
        showDialog();
        XUtil.Post(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: " + result);
                try {
                    JSONObject json = new JSONObject(result);
                    String re = json.optString("result");
                    String info = json.optString("info");
                    MyToast(info);
                    if ("1".equals(re)) {
                        MyApplication.getInstance().removerUser();
                        if ("更换登录密码".equals(type)) {

                        } else {
                            MyApplication.getInstance().userBean.setPhoneNumber(phone);
                        }
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    private String oldphone;
    private String codetime;
    private String first_code;

    private void getCode(String phoneNumber) {
        Map<String, Object> map = new HashMap<>();
        map.put("phoneNumber", phoneNumber);

        showDialog();
        XUtil.Post(ConfigUtil.GETYZM, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.d(TAG, "onSuccess: " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("result");
                    if ("1".equals(re)) {
                        JSONObject jsonObject1 = jsonObject.optJSONObject("info");
                        oldphone = jsonObject1.optString("phone");
                        codetime = jsonObject1.optString("time");
                        first_code = jsonObject1.optString("code");
                        time = 60;
                        handler.sendEmptyMessage(999);
                    } else {
                        String info = jsonObject.optString("info");
                        MyToast(info);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

        phone = s.toString();
        if (MyUtils.isMobileNO(phone)) {
            if (time == 0) {
                get_verification_code_tv.setSelected(true);
            } else {
                get_verification_code_tv.setSelected(false);
            }
        } else {
            get_verification_code_tv.setSelected(false);
        }
    }
}
